use actix_web::web::Json;
use serde::Serialize;

#[derive(Serialize)]
pub struct R<'a, T> {
    code: u16,
    msg: &'a str,
    data: T
}

pub fn data<T>(data: T, msg: &str) -> Json<R<T>> {
    Json(R {
        code: 1,
        msg,
        data
    })
}

pub fn err(code: u16, msg: &str) -> Json<R<String>> {
    Json(R {
        code,
        msg,
        data: "".to_string()
    })
}

pub fn error(error: &str) -> Json<R<String>> {
    Json(R {
        code: 0,
        msg: error,
        data: "".to_string()
    })
}
pub fn success(msg: &str) -> Json<R<String>> {
    Json(R {
        code: 1,
        msg,
        data: "".to_string()
    })
}
pub fn info<'a>(result: bool, ok_text: &'a  str, err_text: &'a  str) -> Json<R<'a, String>> {
    match result {
        true => success(ok_text),
        false => error(err_text)
    }
}

    